﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AcademiaInteligenteWeb
{
    public partial class ConsultarCliente : System.Web.UI.Page
    {

        private DataSet conexaoDataSet;
        private SqlConnection conexao;

        protected void Page_Load(object sender, EventArgs e)
        {
            conexaoDataSet = new DataSet();
            conexao = new SqlConnection("Data Source=186.202.128.216;Initial Catalog=DBDev03;User ID=dbdev03;Password=naruto");

            if (!IsPostBack)
            {
                grdConsultarCliente.DataKeyNames = new string[] { "id" };
                AtualizarGrid();
            }
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            try
            {

                string sql = "SELECT * FROM Cliente" + (DropDownCliente.SelectedIndex > 0 ? " where " + DropDownCliente.SelectedItem.Value + " like '%" + txtPesquisa.Text + "%'" : string.Empty);

                conexao.Open();

                SqlCommand cmd = new SqlCommand(sql, conexao);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable cliente = new DataTable();
                da.Fill(cliente);
                grdConsultarCliente.DataSource = cliente;
                grdConsultarCliente.DataBind();
            }

            catch (Exception ex)
            {

            }
            finally
            {
                conexao.Close();
            }


        }

        public void AtualizarGrid()
        {
            grdConsultarCliente.DataSource = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().ListarCliente();
            grdConsultarCliente.DataBind();
        }

    }
}